<?php

namespace app\index\controller;

use think\Request;
use think\Db;

class User extends Common 
{
	public function index() 
	{	dump(UID);
		$info = DB::name('user')->field('username, sex, email')->where('id', UID)->find();
		$this->assign('info', $info);
		return $this->fetch();
	}

	public function address() 
	{	
		$list = DB::name('address')
			->field('a.id as id, a.province as provinceId, a.city as cityId ,a.county as countyId, b.areaname as province, c.areaname as city, d.areaname as county, address, consignee, mobile')
			->alias('a')
			->join('__AREA__ b', 'a.province = b.id', 'LEFT')
			->join('__AREA__ c', 'a.city = c.id', 'LEFT')
			->join('__AREA__ d', 'a.county = d.id', 'LEFT')
			->where('uid', UID)
			->order('is_default DESC')
			->order('id ASC')
			->select();
		$this->assign('list', $list);
		return $this->fetch();
	}

	public function editPassword()
	{	
		$oldPassword = input('post.oldPassword');
		$newPassword = input('post.newPassword');

		$salt = DB::name('user')->where('id', UID)->find('salt');
		$newPassword;
		DB::name('user')->where('id', UID)->update('password', $newPassword);
	}

	public function editInfomation()
	{

	}


    /**
     * ajax设置个人信息
     * 
     */
    public function setInfo()
    {
    	if (Request::instance()->isPost()) {
    		$nickname = input('post.nickname');
    		$sex      = input('post.sex');
    		$birthday = input('post.birthday');
    		$truename = input('post.truename');

    		$data['nickname'] = $nickname;
    		$data['sex']      = $sex;
    		$data['birthday'] = $birthday;
    		$data['truename'] = $truename;

    		$num = DB::name('user')->where('id', UID)->update($data);
    		if ($num) {
    			return $this->success('更新成功');
    		} else {
    			return $this->error('更新失败');
    		}
    	}
    }

	// 最后利用api实现
	// public function bindPhone()
	// {

	// }

	// public function bindEmail()
	// {

	// }
}
